package IMDB;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Dennis
 */
public class IMDB_moviePage {

    private String IMDBID, plotOutline, fullPlot, fullCredits, releaseInfo;
    private ArrayList<String> genres, stars;
    private int runTime;
    private double rating;
    
    
    public IMDB_moviePage () {
        this.IMDBID = "";
        this.plotOutline = "";
        this.fullPlot = "";
        this.fullCredits = "";
        this.releaseInfo = "";
        this.genres = new ArrayList<String>();
        this.stars = new ArrayList<String>();
    }
    
    private void cleanVariables () {
        this.genres.clear();
        this.stars.clear();
        this.IMDBID = "";
        this.plotOutline = "";
        this.fullPlot = "";
        this.fullCredits = "";
        this.releaseInfo = "";
        this.rating = 0;
        this.runTime = 0;
    }
    
    public void startFetch(URL imdbURL) {
        try {
            
            cleanVariables();
            
            this.IMDBID = imdbURL.toString().substring(imdbURL.toString().lastIndexOf("/") + 1, imdbURL.toString().length());
            this.releaseInfo = imdbURL.toString() + "/releaseinfo";
            this.fullCredits = imdbURL.toString() + "/fullcredits";
            
            HttpURLConnection conn = (HttpURLConnection)imdbURL.openConnection();
            conn.setAllowUserInteraction(false);         
            conn.setDoOutput(true);
            conn.addRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31");
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                
                /*
                 * GENRES
                 * GENRES
                 * GENRES
                 */
                if(inputLine.indexOf("<h4 class=\"inline\">Genres:</h4>") != -1) {
                    inputLine = in.readLine();
                    do  {
                        genres.add(inputLine.substring(inputLine.indexOf("stry_gnr\" >")+ 12, inputLine.indexOf("</a>")));
                        inputLine = in.readLine();
                    } while (inputLine.indexOf("</div>") == -1);
                }
                
                /*
                 * STARS
                 * STARS
                 * STARS
                 */
                else if (inputLine.indexOf("<h4 class=\"inline\">Stars:</h4>") != -1) {
                    do  {
                        inputLine = in.readLine();
                        stars.add(inputLine.substring(inputLine.indexOf("<a href=\"/name/")+ 15, inputLine.indexOf("/?ref")));
                    } while (inputLine.indexOf("class=\"ghost\"") == -1);
                }
                
                /*
                 * RUNTIME
                 * RUNTIME
                 * RUNTIME
                 */
                else if (inputLine.indexOf("class=\"inline\">Runtime") != -1) {
                    inputLine = in.readLine();
                    runTime = Integer.parseInt(inputLine.substring(inputLine.indexOf(">") + 1, inputLine.indexOf(" min")));
                }
                
                /*
                 * RATING
                 * RATING
                 * RATING
                 */
                else if (inputLine.indexOf("span itemprop=\"ratingValue\"") != -1) {
                    //System.out.println("\n\n-----------------\n");
                    //System.out.println(inputLine);
                    //System.out.println(inputLine.indexOf(">", inputLine.indexOf("ratingValue")) + 1);
                    //System.out.println(inputLine.indexOf("<", inputLine.indexOf("ratingValue")));
                    //System.out.println("\n-----------------\n\n");
                    //System.out.println("Rating: " + Double.parseDouble(inputLine.substring(inputLine.indexOf(">", inputLine.indexOf("ratingValue")) + 1, inputLine.indexOf("<", inputLine.indexOf("ratingValue")))));
                    rating = Double.parseDouble(inputLine.substring(inputLine.indexOf(">", inputLine.indexOf("ratingValue")) + 1, inputLine.indexOf("<", inputLine.indexOf("ratingValue"))));
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    
    public String getIMDBID () {
        return IMDBID;
    }
    
    public int getRuntime () {
        return runTime;
    }
    
    public Double getRating () {
        return rating;
    }
    
    public String getPlotOutline () {
        return plotOutline;
    }
    
    public String getFullPlot () {
        return fullPlot;
    }
    
    public URL getReleaseInfoURL () throws MalformedURLException {
        return new URL(releaseInfo);
    }
    
    public URL getFullCreditsURL () throws MalformedURLException {
        return new URL(fullCredits);
    }
    
    public ArrayList<String> getStars () {
        return stars;
    }
    
    public ArrayList<String> getGenres () {
        return genres;
    }
}
